package top.codedance.iotp.registy.net.handle.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.registy.RegistryApplication;
import top.codedance.iotp.registy.net.handle.AdapterController;
import org.apache.log4j.Logger;

import java.util.*;

public class InformationController extends AdapterController {
    private static Logger logger = RegistryApplication.getLogger(InformationController.class);

    public String doSubHandler(JSONObject body) {
        try {
            JSONObject data = new JSONObject();
            JSONObject jsonObject = new JSONObject();
            for(String s : RegistryApplication.servers.keySet()){
                jsonObject.put(s, new JSONArray());
            }
            Set<Map.Entry<String, String>> entries = RegistryApplication.clients.entrySet();
            for(Map.Entry<String, String> entry : entries){
                try {
                    String value = entry.getValue();
                    JSONArray jsonArray = jsonObject.getJSONArray(value);
                    jsonArray.add(entry.getKey());
                    jsonObject.put(value, jsonArray);
                }catch (Exception ex){}
            }
            data.put("cluster", jsonObject);
            data.put("version", RegistryApplication.version);
            return okResponse(data);
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return response500( "Programe exception");
        }
    }
}
